package com.jaya.budan.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jaya.budan.R;
import com.jaya.budan.adapter.GeRen_Main_Fans_ListView_Adapter;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.bean.User_Limit;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;
import com.jaya.budan.views.CommonPopWindow;
import com.jaya.budan.views.MyListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class GeRen_Main_Fans_Quiet_Activity extends BaseActivity implements View.OnClickListener, CommonPopWindow.ViewClickListener {

    private RelativeLayout back;
    private String access_token;
    private int uid, position_1;
    private TextView tv_feedback_tijiao;
    private MyListView lv_xyf_jilu;
    private GeRen_Main_Fans_ListView_Adapter adapter;
    private List<User_Limit> user_limit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_geren_main_fans_quiet);
        Intent intent = getIntent();
        access_token = intent.getStringExtra("access_token");
        uid = intent.getIntExtra("uid", 0);
        back = findViewById(R.id.back);
        tv_feedback_tijiao = findViewById(R.id.tv_feedback_tijiao);
        lv_xyf_jilu = findViewById(R.id.lv_xyf_jilu);
        back.setOnClickListener(this);
        tv_feedback_tijiao.setOnClickListener(this);
        postData();// 1 粉丝 2 关注 3 悄悄关注
        lv_xyf_jilu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ToolUtils.showToast(GeRen_Main_Fans_Quiet_Activity.this, "" + position);
                Button tv_nearby_listview_item_online = view.findViewById(R.id.tv_nearby_listview_item_online);
                tv_nearby_listview_item_online.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        position_1 = position;
                        CommonPopWindow.newBuilder()
                                .setView(R.layout.pop_geren_fans_cancle)
                                .setAnimationStyle(R.style.AnimUp)
                                .setBackgroundDrawable(new BitmapDrawable())
                                .setSize(0, 1500)
                                .setViewOnClickListener(GeRen_Main_Fans_Quiet_Activity.this)
                                .setBackgroundDarkEnable(true)
                                .setBackgroundAlpha(0.7f)
                                .build(GeRen_Main_Fans_Quiet_Activity.this)
                                .showAsCenter(view);
                    }
                });
            }
        });
        lv_xyf_jilu.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                ToolUtils.showToast(GeRen_Main_Fans_Quiet_Activity.this, "" + position);
                return false;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_feedback_tijiao:
                ToolUtils.showToast(GeRen_Main_Fans_Quiet_Activity.this, "清空clear");
                //清空clear
                lv_xyf_jilu.setAdapter(null);
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postData() {
        Log.d("main-list", uid + "");
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.lt_fans_list_Url + access_token + "&page=1&limit=20&uid=" + uid + "&type=3", null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("main_list", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                        user_limit = gson.fromJson(info.toString(), new TypeToken<List<User_Limit>>(){}.getType());
                        adapter = new GeRen_Main_Fans_ListView_Adapter(GeRen_Main_Fans_Quiet_Activity.this, user_limit, 1, 0);
                        lv_xyf_jilu.setAdapter(adapter);
                    } else {
                        ToolUtils.showToast(GeRen_Main_Fans_Quiet_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(GeRen_Main_Fans_Quiet_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    @Override
    public void getChildView(final PopupWindow mPopupWindow, View view, int mLayoutResId) {
        switch (mLayoutResId) {
            case R.layout.pop_geren_fans_cancle:
                TextView tv_cancle = view.findViewById(R.id.tv_cancle);
                TextView img_guanbi = view.findViewById(R.id.img_guanbi);
                tv_cancle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                    }
                });
                img_guanbi.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                        user_limit.remove(position_1);
                        adapter.notifyDataSetChanged();
                    }
                });
                break;
            default:
                break;
        }
    }
}
